﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SharpSoft.WPF.CommonUI
{
    /// <summary>
    /// MultimediaControl1.xaml 的交互逻辑
    /// </summary>
    public partial class MultimediaControl1 : UserControl
    {
        public MultimediaControl1()
        {
            InitializeComponent();
            this.Unloaded += MultimediaPage_Unloaded;
            this.Loaded += MultimediaPage_Loaded;
        }

        private void MultimediaPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (ui is MediaElement video)
            {
                video.Stop();
                if (this.AutoPlay)
                {
                    video.Play();
                }

            }
        }

        private void MultimediaPage_Unloaded(object sender, RoutedEventArgs e)
        {
            if (ui is MediaElement video)
            {
                video.Stop();
            }
        }


        /// <summary>
        /// 媒体类型
        /// </summary>
        public MediaType MediaType
        {
            get { return (MediaType)GetValue(MediaTypeProperty); }
            set { SetValue(MediaTypeProperty, value); }
        }

        public static readonly DependencyProperty MediaTypeProperty =
            DependencyProperty.Register("MediaType", typeof(MediaType), typeof(MultimediaControl1), new PropertyMetadata(MediaType.None));


        /// <summary>
        /// 是否自动开始播放，仅媒体类型为视频时有效。
        /// </summary>
        public bool AutoPlay
        {
            get { return (bool)GetValue(AutoPlayProperty); }
            set { SetValue(AutoPlayProperty, value); }
        }

        public static readonly DependencyProperty AutoPlayProperty =
            DependencyProperty.Register("AutoPlay", typeof(bool), typeof(MultimediaControl1), new PropertyMetadata(true));


        /// <summary>
        /// 是否循环播放，仅媒体类型为视频时有效。
        /// </summary>
        public bool Cycleplay
        {
            get { return (bool)GetValue(CycleplayProperty); }
            set { SetValue(CycleplayProperty, value); }
        }

        public static readonly DependencyProperty CycleplayProperty =
            DependencyProperty.Register("Cycleplay", typeof(bool), typeof(MultimediaControl1), new PropertyMetadata(true));


        /// <summary>
        /// 媒体内容的拉伸方式
        /// </summary>
        public Stretch Stretch
        {
            get { return (Stretch)GetValue(StretchProperty); }
            set { SetValue(StretchProperty, value); }
        }

        public static readonly DependencyProperty StretchProperty =
            DependencyProperty.Register("Stretch", typeof(Stretch), typeof(MultimediaControl1), new PropertyMetadata(Stretch.Uniform));




        /// <summary>
        /// 媒体源
        /// </summary>
        public Uri Source
        {
            get { return (Uri)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(Uri), typeof(MultimediaControl1), new PropertyMetadata(null));

        /// <summary>
        /// 自定义UI内容，指定自定义UI后MediaType不再生效
        /// </summary>


        public UIElement CustomeUI
        {
            get { return (UIElement)GetValue(CustomeUIProperty); }
            set { SetValue(CustomeUIProperty, value); }
        }

        public static readonly DependencyProperty CustomeUIProperty =
            DependencyProperty.Register("CustomeUI", typeof(UIElement), typeof(MultimediaControl1), new PropertyMetadata(null));



        private UIElement ui;

        protected void OnResetMediaType()
        {
            ui = null;
            grid.Children.Clear();
            if (CustomeUI == null)
            {
                switch (this.MediaType)
                {
                    case MediaType.None:
                        break;
                    case MediaType.Image:
                        ui = new Image() { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch };
                        grid.Children.Add(ui);
                        break;
                    case MediaType.Video:
                        var video = new MediaElement() { IsMuted = false, Volume = 1, Balance = 1, LoadedBehavior = MediaState.Manual, UnloadedBehavior = MediaState.Manual, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch };
                        video.MediaOpened += Video_MediaOpened;
                        video.MediaEnded += Video_MediaEnded;
                        ui = video;
                        grid.Children.Add(ui);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                grid.Children.Add(CustomeUI);
            }

            this.OnResetSource();
            this.OnResetStretch();
        }
        protected void OnResetSource()
        {
            this.MediaType = GetMediaType(this.Source.ToString());
            if (ui is Image img)
            {
                if (this.Source == null)
                {
                    img.Source = null;
                }
                else
                {
                    img.Source = new BitmapImage(this.Source);
                }

            }
            else if (ui is MediaElement video)
            {
                video.Source = this.Source;
                if (video.Source != null)
                {
                    if (this.AutoPlay)
                    {
                        video.Play();
                    }
                }
            }
        }
        private MediaType GetMediaType(string filename)
        {
            var ext = System.IO.Path.GetExtension(filename);
            if (ext != null)
            {
                ext = ext.ToLower();
                switch (ext)
                {
                    case ".jpg":
                    case ".png":
                    case ".bmp":
                    case ".jpeg":
                        return MediaType.Image;
                    case ".mp4":
                    case ".wmv":
                    case ".avi":
                    case ".mkv":
                    case ".flv":
                        return MediaType.Video;
                    default:
                        break;
                }
            }
            return MediaType.None;
        }
        protected void OnResetStretch()
        {
            if (ui is Image img)
            {
                img.Stretch = this.Stretch;
            }
            else if (ui is MediaElement video)
            {
                video.Stretch = this.Stretch;
            }
        }
        private void Video_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (this.Cycleplay)
            {
                var video = (MediaElement)sender;
                video.Stop();
                video.Play();
            }
        }

        private void Video_MediaOpened(object sender, RoutedEventArgs e)
        {
            if (this.AutoPlay)
            {
                var video = (MediaElement)sender;
                video.Play();
            }
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == MediaTypeProperty || e.Property == CustomeUIProperty)
            {
                this.OnResetMediaType();
            }
            else if (e.Property == SourceProperty)
            {
                this.OnResetSource();
            }
            else if (e.Property == StretchProperty)
            {
                this.OnResetStretch();
            }
        }
    }
}
